<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告类型" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择广告类型" clearable size="small">
          <el-option
            v-for="dict in contentTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:adinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:adinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:adinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:adinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adinfoList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="图片或视频名称" align="center" prop="contentName" />
      <el-table-column label="广告类型" align="center" prop="contentType" :formatter="contentTypeFormat" />
      <el-table-column label="广告链接地址" align="center" prop="contentUrl" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text" v-if="scope.row.contentType == 'I'"
            icon="el-icon-picture"
            @click="showImg(scope.row.contentUrl)"
          >查看图片</el-button>
          <el-button
            size="mini"
            type="text" v-else
            icon="el-icon-video-camera"
            @click="showVideo(scope.row.contentUrl)"
          >点击播放</el-button>
        </template>
      </el-table-column>
      <el-table-column label="广告时长(秒)" align="center" prop="duration" />
      <el-table-column label="有效期开始时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投放设备编号" align="center" prop="deviceNo" >
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="300"
            trigger="hover"
            :disabled="scope.row.deviceNo.length <= 8"
          >
            <div>{{ scope.row.deviceNo }}</div>
            <span slot="reference" v-if="scope.row.deviceNo.length <= 10">{{scope.row.deviceNo}}</span>
            <span slot="reference" v-if="scope.row.deviceNo.length > 10">{{scope.row.deviceNo.substr(0, 10) + "..."}}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="投放状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:adinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:adinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门禁广告管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="图片或视频名称" prop="contentName">
          <el-input v-model="form.contentName"  placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="广告类型" prop="contentType">
          <el-select v-model="form.contentType" placeholder="请选择广告类型">
            <el-option
              v-for="dict in contentTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传图片" prop="contentUrl" v-if="form.contentType == 'I'">
          <upload-img v-model="form.contentUrl" :limit="9"></upload-img>
        </el-form-item>
        <el-form-item label="上传视频" prop="contentUrl" v-if="form.contentType == 'V'">
          <file-upload :fileType="fileType" :urls="form.contentUrl" :fileSize="20" @input="getVideo" ></file-upload>
        </el-form-item>
        <el-form-item label="播放持续时长(秒)" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入播放持续时长(秒)" />
        </el-form-item>
        <el-form-item label="生效时间" prop="beginTime">
          <el-date-picker clearable size="small"
            v-model="form.beginTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择生效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="门禁设备编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 查看图片 -->
    <el-dialog :title="title" :visible.sync="openPhotos" v-if="openPhotos" width="60%" append-to-body>
      <show-imgs :imgs="photos"></show-imgs>
    </el-dialog>
    <!-- 查看视频 -->
    <el-dialog :title="title" :visible.sync="openVideo" v-if="openVideo" width="60%" append-to-body>
      <video :src="video" autoplay controls  ></video>
    </el-dialog>



  </div>
</template>

<script>
import { listAdinfo, getAdinfo, delAdinfo, addAdinfo, updateAdinfo, exportAdinfo } from "@/api/system/adinfo";
import moreImgUpload from '@/components/moreImgUpload';
import FileUpload from '@/components/FileUpload';
import UploadImg from "@/components/UploadImg";
import ShowImgs from "@/components/ShowImgs";
export default {
  name: "Adinfo",
  components: {
    ShowImgs,
    UploadImg,
    moreImgUpload,
    FileUpload
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //打开图片
      openPhotos: false,
      //图片列表
      photos:"",
      //视频格式
      fileType:['mp4', 'm2v', 'mkv'],
      //打开视频
      openVideo: false,
      //视频地址
      video:"",
      // 总条数
      total: 0,
      // 门禁广告管理表格数据
      adinfoList: [],
      // 弹出层标题
      title: "",
      //视频播放参数
      playerOptions:{},
      // 是否显示弹出层
      open: false,
      // 广告类型[I:图片 V:视频]字典
      contentTypeOptions: [],
      // 广告下发状态0未下发  1已下发字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contentType: undefined,
        contentUrl: undefined,
        duration: undefined,
        beginTime: undefined,
        endTime: undefined,
        msgId: undefined,
        deviceNo: undefined,
        deviceNoBox: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("adinfo_content_type").then(response => {
      this.contentTypeOptions = response.data;
    });
    this.getDicts("adinfo_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询门禁广告管理列表 */
    getList() {
      this.loading = true;
      listAdinfo(this.queryParams).then(response => {
        this.adinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 广告类型[I:图片 V:视频]字典翻译
    contentTypeFormat(row, column) {
      return this.selectDictLabel(this.contentTypeOptions, row.contentType);
    },
    // 广告下发状态0未下发  1已下发字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openPhotos = false;
      this.openVideo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        contentName: undefined,
        contentType: undefined,
        contentUrl: undefined,
        duration: undefined,
        beginTime: undefined,
        endTime: undefined,
        msgId: undefined,
        deviceNo: undefined,
        deviceNoBox: undefined,
        status: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
      this.video = '';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁广告管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getAdinfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改门禁广告管理";
      });
    },
    getVideo(url){
      this.form.contentUrl=url;
      this.form.contentName = this.getFileName(url)
    },
    // 获取文件名称
    getFileName(name) {
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
      } else {
        return "";
      }
    },
    /** 查看图片 */
    showImg(Img) {
      this.openPhotos = true;
      this.photos = Img
      this.title = "图片列表";
    },
    /** 查看图片 */
    showVideo(video) {
      this.reset();
      this.openVideo = true;
      if (video){
        this.video = video;
        /*this.playerOptions= {
          playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
          autoplay: false, //如果true,浏览器准备好时开始回放。
          controls: true, //控制条
          preload: 'auto', //视频预加载
          muted: false, //默认情况下将会消除任何音频。
          loop: false, //导致视频一结束就重新开始。
          language: 'zh-CN',
          aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
          fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
          sources: [{
            type: 'video/mp4',
            src: video//你所放置的视频的地址，最好是放在服务器上
          }],
          // poster: "http://39.106.117.192:8080/static/indexImg.png", //你的封面地址（覆盖在视频上面的图片）
          width: document.documentElement.clientWidth,
          notSupportedMessage: '此视频暂无法播放，请稍后再试' //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        }*/
      }

      this.title = "视频播放";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateAdinfo(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdinfo(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除门禁广告管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delAdinfo(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有门禁广告管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAdinfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
